
#include <QApplication>

#include "mainwindow.h"

#include "utils.h"
#include "loghandler.h"

#ifndef APP_VERSION
#define APP_VERSION "0.0.0"
#endif

#ifndef APP_BIN_NAME
#define APP_BIN_NAME ""
#endif

#ifndef APP_QM_NAME
#define APP_QM_NAME ""
#endif

void ExitSignalHandler(int signal)
{
    Q_UNUSED(signal)
    qApp->quit();
}

static void AppCleanup()
{
    FUNCTION_TRACE;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QApplication::setApplicationName(APP_BIN_NAME);
    QApplication::setApplicationVersion(APP_VERSION);

    qAddPostRoutine(AppCleanup);

    Utils &utils = Utils::GetInstance();
    utils.HandelSignal(SIGINT, ExitSignalHandler);
    utils.HandelSignal(SIGTERM, ExitSignalHandler);
    utils.AttachOutputToConsole();

    LogHandler::InstallLogHandler(LogHandler::LogMode::Debug);

    bool gui_mode = true;
    QString Cmd;
    if (argc > 1) {
        Cmd = QString(argv[1]).toLower();
        if (Cmd == "cli") {
            gui_mode = false;
        }
    }

    if (gui_mode) {
        MainWindow w;
        w.show();
    } else {
        utils.ExitAfter(100);
    }

    return a.exec();
}
